import 'package:get/get.dart';

import '../../../data/catergory_model.dart';
import '../../../net/HttManager.dart';
import '../../../net/Url.dart';

class CategoryController extends GetxController {
  final count = 0.obs;

  RxInt currentPosition = 0.obs;

  RxList<CatergoryItem> mCatergoryItems = <CatergoryItem>[].obs;

  RxMap mCatergoryGoodMaps = RxMap();

  RxInt mCatergoryGoodMapsSize = 0.obs;

  @override
  void onInit() {
    super.onInit();
    catergoryList();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  ///商品分类
  void catergoryList() async {
    var rsp = await HttManager.instance.get(Url.API_PCATE);
    if (rsp != null && rsp.data != null) {
      mCatergoryItems.value = CatergoryModel.fromJson(rsp.data).result;
      update();
      setCurrentPosition(0);
    }
  }

  ///根据分类查询商品
  void catergoryGoods(String pid) async {
    var map = {"pid": pid};
    var rsp = await HttManager.instance.get(Url.API_PCATE, map: map);
    if (rsp != null && rsp.data != null) {
      mCatergoryGoodMaps.value = rsp.data;
      update();
    }
  }

  void setCurrentPosition(int position) {
    print('setCurrentPosition position = ${position}');
    currentPosition.value = position;
    update();
    String sid = mCatergoryItems.value[position].sid;
    print('setCurrentPosition pid = ${sid}');
    catergoryGoods(sid);
  }
}
